#include "FrontRibbonCtrlState.h"
#include "Animations.h"
#include "LampManager.h"
#include "F3LampInterfaces.h"

RibbonLoBeamCtrlStateMachine_On g_RibbonLoBeamCtrlStateMachine_On;

void InitRibbonLoBeamCtrlStateMachineOn(void)
{

    g_RibbonLoBeamCtrlStateMachine_On.ribbonLoBeamEffectStateMachineDefault_.sm_.Entry = RibbonLoBeamEffectStateMachineDefaultEntry;
    g_RibbonLoBeamCtrlStateMachine_On.ribbonLoBeamEffectStateMachineDefault_.sm_.Excute = RibbonLoBeamEffectStateMachineDefaultExcute;
    g_RibbonLoBeamCtrlStateMachine_On.ribbonLoBeamEffectStateMachineDefault_.sm_.Exit = RibbonLoBeamEffectStateMachineDefaultExit;
    g_RibbonLoBeamCtrlStateMachine_On.ribbonLoBeamEffectStateMachineDefault_.sm_.waitExitCount_ = 0;
    g_RibbonLoBeamCtrlStateMachine_On.ribbonLoBeamEffectStateMachineUnlock_.sm_.Entry = RibbonLoBeamEffectStateMachineUnlockEntry;
    g_RibbonLoBeamCtrlStateMachine_On.ribbonLoBeamEffectStateMachineUnlock_.sm_.Excute = RibbonLoBeamEffectStateMachineUnlockExcute;
    g_RibbonLoBeamCtrlStateMachine_On.ribbonLoBeamEffectStateMachineUnlock_.sm_.Exit = RibbonLoBeamEffectStateMachineUnlockExit;
    g_RibbonLoBeamCtrlStateMachine_On.ribbonLoBeamEffectStateMachineUnlock_.sm_.waitExitCount_ = 0;

    g_RibbonLoBeamCtrlStateMachine_On.ribbonLoBeamEffectStateMachineLock_.sm_.Entry = RibbonLoBeamEffectStateMachineLockEntry;
    g_RibbonLoBeamCtrlStateMachine_On.ribbonLoBeamEffectStateMachineLock_.sm_.Excute = RibbonLoBeamEffectStateMachineLockExcute;
    g_RibbonLoBeamCtrlStateMachine_On.ribbonLoBeamEffectStateMachineLock_.sm_.Exit = RibbonLoBeamEffectStateMachineLockExit;
    g_RibbonLoBeamCtrlStateMachine_On.ribbonLoBeamEffectStateMachineLock_.sm_.waitExitCount_ = 0;

    g_RibbonLoBeamCtrlStateMachine_On.sm_.Entry = RibbonLoBeamCtrlStateMachineOnEntry;
    g_RibbonLoBeamCtrlStateMachine_On.sm_.Excute = RibbonLoBeamCtrlStateMachineOnExcute;
    g_RibbonLoBeamCtrlStateMachine_On.sm_.Exit = RibbonLoBeamCtrlStateMachineOnExit;
    g_RibbonLoBeamCtrlStateMachine_On.sm_.waitExitCount_ = 0;
}

void RibbonLoBeamCtrlStateMachineInitEntry(void)
{
    // std::cout << "Base::RibbonLoBeamCtrlStateMachineInitEntry" << std::endl;
}

void RibbonLoBeamCtrlStateMachineInitExcute(void)
{
    // std::cout << "Base::RibbonLoBeamCtrlStateMachineInitExcute" << std::endl;
}

void RibbonLoBeamCtrlStateMachineInitExit(void)
{
    // std::cout << "Base::RibbonLoBeamCtrlStateMachineInitExit" << std::endl;
}


void RibbonLoBeamCtrlStateMachineOffEntry(void)
{
    // std::cout << "Base::RibbonLoBeamCtrlStateMachineOffEntry" << std::endl;
    resetLobeamLampRibbonUnlockAnimation();
    resetLobeamLampRibbonLockAnimation();
}

void RibbonLoBeamCtrlStateMachineOffExcute(void)
{
    // std::cout << "Base::RibbonLoBeamCtrlStateMachineOffExcute" << std::endl;
    // handle loboam / dlp / hibeam / po2 off on cmd
}

void RibbonLoBeamCtrlStateMachineOffExit(void)
{
    // std::cout << "Base::RibbonLoBeamCtrlStateMachineOffExit" << std::endl;
}

void RibbonLoBeamEffectStateMachineUnlockEntry(void)
{
    // std::cout << "Base::Ctrl::RibbonLoBeamEffectStateMachineUnlockEntry" << std::endl;
    g_Po2LedView.model_->onOffStatus_ = OnOff_Off;
    g_DlpModelView.model_->DO_S32K_SL3409_PWM_ = 0;
    g_DlpModelView.model_->isLM3409_EN_Enable_ = false;
    g_HiBeamView.model_->onOffStatus_ = OnOff_Off;
    g_LoBeamView.model_->onOffStatus_ = OnOff_Off;
    g_CornerLampView.model_->onOffStatus_ = OnOff_Off;
}

void RibbonLoBeamEffectStateMachineUnlockExcute(void)
{
    // std::cout << "Base::Ctrl::RibbonLoBeamEffectStateMachineUnlockExcute" << std::endl;
    switch(g_RibbonAnimationModel.lobeamMode_)
    {
        case Init:
        {
            g_RibbonView.model_->frntRibbonLampEffectShowSt_ = RibbonEffectShow_OFF;
            g_RibbonView.model_->frntRibbonLampEffectSt_     = RibbonEffectSel_Initial;
        }
        case Pause:
        {
            g_RibbonView.model_->frntRibbonLampEffectShowSt_ = RibbonEffectShow_Pause;
            break;
        }
        default:
        {
            playLobeamLampRibbonUnlockAnimation();
            break;
        }
    }
}

void RibbonLoBeamEffectStateMachineUnlockExit(void)
{
    // std::cout << "Base::Ctrl::RibbonLoBeamEffectStateMachineUnlockExit" << std::endl;
   resetLobeamLampRibbonUnlockAnimation();
   resetLobeamLampRibbonLockAnimation();
}

void RibbonLoBeamEffectStateMachineLockEntry(void)
{
    // std::cout << "Base::Ctrl::RibbonLoBeamEffectStateMachineLockEntry" << std::endl;
    resetLobeamLampRibbonUnlockAnimation();
    // turn dlp and lobeam
    g_LoBeamView.model_->onOffStatus_ = OnOff_On;
    g_LoBeamView.model_->pwm_ = 50.0f;

    g_Po2LedView.model_->onOffStatus_ = OnOff_Off;

    g_DlpModelView.model_->isLM3409_EN_Enable_ = true;
    g_DlpModelView.model_->isDLP_LED1_And_DLP_LED2_Enable_ = true;
    g_DlpModelView.model_->DO_S32K_SL3409_PWM_ = 50;
}

void RibbonLoBeamEffectStateMachineLockExcute(void)
{
    // std::cout << "Base::Ctrl::RibbonLoBeamEffectStateMachineLockExcute" << std::endl;
    switch(g_RibbonAnimationModel.lobeamMode_)
    {
        case Init:
        {
            g_RibbonView.model_->frntRibbonLampEffectShowSt_ = RibbonEffectShow_OFF;
            g_RibbonView.model_->frntRibbonLampEffectSt_     = RibbonEffectSel_Initial;
        }
        case Pause:
        {
            g_RibbonView.model_->frntRibbonLampEffectShowSt_ = RibbonEffectShow_Pause;
            break;
        }
        default:
        {
            playLobeamLampRibbonLockAnimation();
            break;
        }
    }
}

void RibbonLoBeamEffectStateMachineLockExit(void)
{
    // std::cout << "Base::Ctrl::RibbonLoBeamEffectStateMachineLockExit" << std::endl;
    resetLobeamLampRibbonUnlockAnimation();
    resetLobeamLampRibbonLockAnimation();
}

void RibbonLoBeamEffectStateMachineDefaultEntry(void)
{
    // std::cout << "Base::Ctrl::RibbonLoBeamEffectStateMachineDefaultEntry" << std::endl;
    resetLobeamLampRibbonUnlockAnimation();
    resetLobeamLampRibbonLockAnimation();
}

void RibbonLoBeamEffectStateMachineDefaultExcute(void)
{
    // std::cout << "Base::Ctrl::RibbonLoBeamEffectStateMachineDefaultExcute" << std::endl;
}

void RibbonLoBeamEffectStateMachineDefaultExit(void)
{
    // std::cout << "Base::Ctrl::RibbonLoBeamEffectStateMachineDefaultExit" << std::endl;
}

void RibbonLoBeamCtrlStateMachineOnEntry(void)
{
    // std::cout << "Base::RibbonLoBeamCtrlStateMachineOnEntry" << std::endl;
}

static void RibbonLoBeamEffectShowExitWithState(RibbonLoBeamCtrlStateMachine_On* normalStateMachine, RibbonLobeamEffectShowState inputState)
{
    switch(inputState)
    {
        case RibbonLobeamEffectShowState_Default:
        {
            normalStateMachine->ribbonLoBeamEffectStateMachineDefault_.sm_.Exit();
            break;
        }

        case RibbonLobeamEffectShowState_Unlock:
        {
            normalStateMachine->ribbonLoBeamEffectStateMachineUnlock_.sm_.Exit();
            break;
        }

        case RibbonLobeamEffectShowState_Lock:
        {
            normalStateMachine->ribbonLoBeamEffectStateMachineLock_.sm_.Exit();
            break;
        }

        default:
        {
            break;
        }
    }
}

static void RibbonLoBeamEffectShowEntryWithState(RibbonLoBeamCtrlStateMachine_On* normalStateMachine, RibbonLobeamEffectShowState inputState)
{
    switch(inputState)
    {
        case RibbonLobeamEffectShowState_Default:
        {
            normalStateMachine->ribbonLoBeamEffectStateMachineDefault_.sm_.Entry();
            break;
        }

        case RibbonLobeamEffectShowState_Unlock:
        {
            normalStateMachine->ribbonLoBeamEffectStateMachineUnlock_.sm_.Entry();
            break;
        }

        case RibbonLobeamEffectShowState_Lock:
        {
            normalStateMachine->ribbonLoBeamEffectStateMachineLock_.sm_.Entry();
            break;
        }

        default:
        {
            break;
        }
    }
}

static void RibbonLoBeamEffectShowExcuteWithState(RibbonLoBeamCtrlStateMachine_On* normalStateMachine, RibbonLobeamEffectShowState inputState)
{
    switch(inputState)
    {
        case RibbonLobeamEffectShowState_Default:
        {
            normalStateMachine->ribbonLoBeamEffectStateMachineDefault_.sm_.Excute();
            break;
        }

        case RibbonLobeamEffectShowState_Unlock:
        {
            normalStateMachine->ribbonLoBeamEffectStateMachineUnlock_.sm_.Excute();
            break;
        }

        case RibbonLobeamEffectShowState_Lock:
        {
            normalStateMachine->ribbonLoBeamEffectStateMachineLock_.sm_.Excute();
            break;
        }

        default:
        {
            break;
        }
    }
}

void RibbonLoBeamCtrlStateMachineOnExcute(void)
{
    // std::cout << "Base::RibbonLoBeamCtrlStateMachineOnExcute" << std::endl;
    if(g_FrontLastState.ribbonLobeamEffectShowState_ != g_FrontNewState.ribbonLobeamEffectShowState_)
    {
        RibbonLoBeamEffectShowExitWithState(&g_RibbonLoBeamCtrlStateMachine_On, g_FrontLastState.ribbonLobeamEffectShowState_);
        RibbonLoBeamEffectShowEntryWithState(&g_RibbonLoBeamCtrlStateMachine_On, g_FrontNewState.ribbonLobeamEffectShowState_);
    }
    RibbonLoBeamEffectShowExcuteWithState(&g_RibbonLoBeamCtrlStateMachine_On, g_FrontNewState.ribbonLobeamEffectShowState_);

    UpdateRibbonLobeamEffectShowState(g_FrontNewState.ribbonLobeamEffectShowState_, &g_FrontLastState.ribbonLobeamEffectShowState_);
}

void RibbonLoBeamCtrlStateMachineOnExit(void)
{
    // std::cout << "Base::RibbonLoBeamCtrlStateMachineOnExit" << std::endl;
    RibbonLoBeamEffectShowExitWithState(&g_RibbonLoBeamCtrlStateMachine_On, g_FrontNewState.ribbonLobeamEffectShowState_);
    UpdateRibbonLobeamEffectShowState(RibbonLobeamEffectShowState_Invaild, &g_FrontLastState.ribbonLobeamEffectShowState_);
}

void RibbonLoBeamCtrlStateMachineAgainEntry(void)
{
    // std::cout << "Base::RibbonLoBeamCtrlStateMachineAgainEntry" << std::endl;
    resetLobeamLampRibbonUnlockAnimation();
    resetLobeamLampRibbonLockAnimation();
}

void RibbonLoBeamCtrlStateMachineAgainExcute(void)
{
    // std::cout << "Base::RibbonLoBeamCtrlStateMachineAgainExcute" << std::endl;
}

void RibbonLoBeamCtrlStateMachineAgainExit(void)
{
    // std::cout << "Base::RibbonLoBeamCtrlStateMachineAgainExit" << std::endl;
}

